/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.service;


import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.account.core.repository.service.AccountRepository;
import info.peigen.hotpot.business.account.facade.order.create.CreateIntermediateAccountOrder;
import info.peigen.hotpot.business.account.facade.result.create.CreateAccountResult;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.domain.partner.action.PartnerConverter;
import info.peigen.hotpot.business.customer.facade.info.partner.PartnerInfo;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerCreateOrder;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerQueryOrder;
import info.peigen.hotpot.business.customer.facade.result.partner.PartnerCreateResult;
import info.peigen.hotpot.business.customer.facade.result.partner.PartnerQueryResult;
import info.peigen.hotpot.common.service.base.DomainServiceInvoke;
import info.peigen.hotpot.common.service.base.ServiceContext;
import org.noear.solon.annotation.Inject;


/**
 * <b>(PartnerDomainService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/29
 */
@SuppressWarnings("rawtypes")
public class PartnerDomainService extends AbstractCustomerDomainService {

    @Inject
    AccountRepository accountRepository;

    public void register(ServiceContext serviceContext) {
        execute(serviceContext, new DomainServiceInvoke() {

            @Override
            public void invoke(ServiceContext serviceContext) {
                Partner partner = (Partner) new Partner().create(serviceContext.getOrder());
                // 生成中间账户
                genIntermediateAccount((PartnerCreateOrder) serviceContext.getOrder(), partner);
                serviceContext.setDomain(partner);
                PartnerCreateResult result = (PartnerCreateResult) serviceContext.getResult();
                result.setSuccessNotFee();
                result.setInfo(PartnerConverter.convertInfo(partner));
            }

            @Override
            public void handleStoreDataEvent(ServiceContext serviceContext) {
                partnerRepository.store((Partner) serviceContext.getDomain());
            }

        });
    }

    private void genIntermediateAccount(PartnerCreateOrder order, Partner partner) {
        CreateIntermediateAccountOrder createIntermediateAccountOrder = CreateIntermediateAccountOrder.builder()
                .partnerId(partner.getPartnerId())
                .aliasName("合作伙伴[" + order.getPartnerName() + "]中间账户")
                .build();
        CreateAccountResult result = accountService.createIntermediateAccount(createIntermediateAccountOrder);
        if (result.success()) {
            Account account = accountRepository.active(result.getInfo().getAccountId());
            partner.setIntermediateAccount(account);
        }
    }


    public void queryPartnerInfo(ServiceContext serviceContext) {
        execute(serviceContext, new DomainServiceInvoke() {
            @Override
            public void activeDomain(ServiceContext serviceContext) {
                PartnerQueryOrder order   = (PartnerQueryOrder) serviceContext.getOrder();
                Partner           partner = partnerRepository.active(order.getPartnerId());
                serviceContext.setDomain(partner);
            }

            @Override
            public void invoke(ServiceContext serviceContext) {
                PartnerInfo partnerInfo = PartnerConverter.convertInfo((Partner) serviceContext.getDomain());
                ((PartnerQueryResult) serviceContext.getResult()).fromSuccess(partnerInfo);
            }
        });
    }
}
